/*------------------- COPYRIGHT AND TRADEMARK INFORMATION ------------------+
 |                                                                          |
 |    VoCon (R) High SDK                                                    |
 |                                                                          |
 |    (c) 2013 Nuance Communications, Inc. All rights reserved.             |
 |                                                                          |
 |    Nuance, the Nuance logo and VoCon are trademarks and/or registered    |
 |    trademarks of Nuance Communications, Inc. or its affiliates in the    |
 |    United States and/or other countries.                                 |
 |                                                                          |
 +--------------------------------------------------------------------------*/
package com.example.mygame.utils;

import java.util.Hashtable;

import android.util.Log;

public class MyLogger  {
	private static boolean logFlag = true;

	//public final static String tag = "[AppName]";
	private static int logLevel = Log.VERBOSE;
	private static Hashtable<String, MyLogger> sLoggerTable = new Hashtable<String, MyLogger>();
	private String mClassName;

	private static MyLogger xlog;
	private static MyLogger ylog;
	private static MyLogger glog;

	private static final String XXJ = "@xxj@";
	private static final String YANGYI = "&yangyi@";
	private static final String GERRY = "@gerry@";
	
	//private BroadcastReceiver mReceiver;

	private MyLogger(String name) {
		mClassName = name;
	}

	/**
	 * 
	 * @param className
	 * @return
	 */
	@SuppressWarnings("unused")
	private static MyLogger getLogger(String className) {
		MyLogger classLogger = (MyLogger) sLoggerTable.get(className);
		if (classLogger == null) {
			classLogger = new MyLogger(className);
			sLoggerTable.put(className, classLogger);
		}
		return classLogger;
	}

	/**
	 * Purpose:Mark user one
	 * 
	 * @return
	 */
	public static MyLogger xLog() {
		if (xlog == null) {
			xlog = new MyLogger(XXJ);
		}
		return xlog;
	}

	/**
	 * Purpose:Mark user two
	 * 
	 * @return
	 */
	public static MyLogger yLog() {
		if (ylog == null) {
			ylog = new MyLogger(YANGYI);
		}
		return ylog;
	}

	/**
	 * Purpose:Mark user three
	 * 
	 * @return
	 */
	public static MyLogger gLog() {
		if (glog == null) {
			glog = new MyLogger(GERRY);
		}
		return glog;
	}

	/**
	 * Get The Current Function Name
	 * 
	 * @return
	 */
	private String getFunctionName() {
		StackTraceElement[] sts = Thread.currentThread().getStackTrace();
		if (sts == null) {
			return null;
		}
		for (StackTraceElement st : sts) {
			if (st.isNativeMethod()) {
				continue;
			}
			if (st.getClassName().equals(Thread.class.getName())) {
				continue;
			}
			if (st.getClassName().equals(this.getClass().getName())) {
				continue;
			}
			return mClassName + "[ " + Thread.currentThread().getName() + ": "
					+ st.getFileName() + ":" + st.getLineNumber() + " "
					+ st.getMethodName() + " ]";
		}
		return null;
	}

	/**
	 * The Log Level:i
	 * 
	 * @param str
	 */
	public void i(Object str) {
		if (logFlag) {
			if (logLevel <= Log.INFO) {
				Log.i(getFunctionName(), str.toString());
			}
		}
	}

	/**
	 * The Log Level:d
	 * 
	 * @param str
	 */
	public void d(Object str) {
		if (logFlag) {
			if (logLevel <= Log.DEBUG) {
				Log.d(getFunctionName(), str.toString());
			}
		}
	}

	/**
	 * The Log Level:V
	 * 
	 * @param str
	 */
	public void v(Object str) {
		if (logFlag) {
			if (logLevel <= Log.VERBOSE) {
				Log.v(getFunctionName(), str.toString());
			}
		}
	}

	/**
	 * The Log Level:w
	 * 
	 * @param str
	 */
	public void w(Object str) {
		if (logFlag) {
			if (logLevel <= Log.WARN) {
				Log.w(getFunctionName(), str.toString());
			}
		}
	}

	/**
	 * The Log Level:e
	 * 
	 * @param str
	 */
	public void e(Object str) {
		//error always print
		//if (logFlag) {
			if (logLevel <= Log.ERROR) {
				Log.e(getFunctionName(), str.toString());
			//}
		}
	}

	/**
	 * The Log Level:e
	 * 
	 * @param ex
	 */
	public void e(Exception ex) {
		if (logFlag) {
			if (logLevel <= Log.ERROR) {
				Log.e(getFunctionName(), "error", ex);
			}
		}
	}

	/**
	 * The Log Level:e
	 * 
	 * @param log
	 * @param tr
	 */
	public void e(String log, Throwable tr) {
		if (logFlag) {
			String line = getFunctionName();
			Log.e(getFunctionName(), "{Thread:" + Thread.currentThread().getName() + "}"
					+ "[" + mClassName + line + ":] " + log + "\n", tr);
		}
	}
	
	public static void setLogFlag(boolean isLogOut) {
		logFlag = isLogOut;
	}
	
	public static void setLogLevel(int logLevel) {
		MyLogger.logLevel = logLevel;
	}
}
